package cm.util.chart;

import java.awt.Color;
import java.awt.Font;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

public class BarChart {
	public static DefaultCategoryDataset getDataset()
	{
		DefaultCategoryDataset dataset=new DefaultCategoryDataset();
		dataset.addValue(12, "2001年", "技术人员");
		dataset.addValue(13, "2002年", "技术人员");
		dataset.addValue(18, "2003年", "技术人员");
		dataset.addValue(12, "2004年", "技术人员");
		dataset.addValue(21, "2001年", "1人员");
		dataset.addValue(14, "2002年", "1人员");
		dataset.addValue(245, "2003年", "1人员");
		dataset.addValue(45, "2004年", "1人员");
		dataset.addValue(12, "2001年", "人员");
		dataset.addValue(56, "2002年", "人员");
		dataset.addValue(29, "2003年", "人员");
		dataset.addValue(19, "2004年", "人员");
		
		return dataset;
	}
	public static JFreeChart createChart(String title,String categoryLabel,String valueLabel,CategoryDataset dataset)
	{
		JFreeChart chart=ChartFactory.createBarChart3D("title",
				categoryLabel,valueLabel,dataset,PlotOrientation.VERTICAL,
				true,true,false);
		chart.setTitle(new TextTitle(title,new Font("宋体",Font.BOLD,20)));
		CategoryPlot plot = chart.getCategoryPlot(); 
		NumberAxis vn = (NumberAxis) plot.getRangeAxis(); 
		vn.setAutoRangeIncludesZero(true); 
//		DecimalFormat df = new DecimalFormat("#0.00"); 
//		vn.setNumberFormatOverride(df); // 

		CategoryAxis domainAxis = plot.getDomainAxis(); 

		domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
		// Lable 
		Font labelFont = new Font("宋体", Font.BOLD, 12); 

		domainAxis.setLabelFont(labelFont);
		domainAxis.setTickLabelFont(labelFont);

		domainAxis.setMaximumCategoryLabelWidthRatio(1f);
		// domainAxis.setVerticalCategoryLabels(false); 
		plot.setDomainAxis(domainAxis); 

		ValueAxis rangeAxis = plot.getRangeAxis(); 
		rangeAxis.setLabelFont(labelFont);
		rangeAxis.setTickLabelFont(labelFont);
		rangeAxis.setUpperMargin(0.15); 
		rangeAxis.setLowerMargin(0.15); 
		plot.setRangeAxis(rangeAxis); 
		BarRenderer renderer = new BarRenderer(); 
		renderer.setMaximumBarWidth(0.7); 
		renderer.setMinimumBarLength(10); 

		renderer.setBaseOutlinePaint(Color.BLACK); 

		renderer.setSeriesPaint(0, Color.GREEN); 
		renderer.setSeriesPaint(1, new Color(0, 0, 255)); 
		renderer.setItemMargin(0.25); 
		renderer 
		.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); 
		renderer.setBaseItemLabelsVisible(true); 

		plot.setRenderer(renderer); 
		plot.setForegroundAlpha(0.6f); 
		plot.setBackgroundAlpha(0.7f);
		
		chart.getLegend().setItemFont(labelFont);

		return chart;
	}
	public static void main(String args[])
	{
		ChartFrame chartFrame=new ChartFrame("企业人员条形图",createChart("企业人员条形图","人员","数量",getDataset()));
		chartFrame.pack();
		chartFrame.setVisible(true);
	}
}
